package com.zl.smartmall.library.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.po.ProvinceInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.vo.NewAddressDao;

public abstract class GetAddresssFromServerListener extends BaseListener {
	private int mReqType;
	private int mCurrentPid;

	
	public int getmReqType() {
		return mReqType;
	}

	public void setmReqType(int mReqType) {
		this.mReqType = mReqType;
	}

	public int getmCurrentPid() {
		return mCurrentPid;
	}

	public void setmCurrentPid(int mCurrentPid) {
		this.mCurrentPid = mCurrentPid;
	}

	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		try {
			int ret = response.getInt("ret");
			switch (ret) {
			case Constants.REQ_OK:
				List<ProvinceInfo> mDataTemp = new ArrayList<ProvinceInfo>();
				JSONArray result = response.getJSONArray("result");
				try {
					for (int i = 0; i < result.length(); i++) {
						ProvinceInfo info = new ProvinceInfo();
						JSONObject resultObj = result.getJSONObject(i);
						info.setName(resultObj.getString("name"));
						info.setCityId(resultObj.getInt("id"));
						info.setType(mReqType);
						info.setPid(mCurrentPid);
						mDataTemp.add(info);
					}
					onResponseSuccess(mDataTemp);
				} catch (JSONException e) {
					e.printStackTrace();
					onResponseFailed();
				}
				break;
			case Constants.REQ_OFFLINE:
				onOffline();
				break;
			case Constants.REQ_NO_MORE_DATA:
				onNoMoreData();
				break;

			default:
				onResponseFailed();
				break;
			}
		} catch (JSONException e) {
			e.printStackTrace();
			onResponseFailed();
		}
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			String responseString, Throwable throwable) {
		super.onFailure(statusCode, headers, responseString, throwable);

	}

	public abstract void onResponseSuccess(List<ProvinceInfo> infos);
}
